package com.evan.review.architecture.mvvm.repository;

import android.app.Application;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import com.evan.review.architecture.mvvm.model.Note;
import com.evan.review.architecture.mvvm.model.NoteDao;
import com.evan.review.architecture.mvvm.model.NoteDatabase;

import java.util.List;

/**
 * 笔记仓库
 * MVVM架构中的Repository层
 * 作为数据源（本地数据库、网络API等）和ViewModel之间的中介
 */
public class NoteRepository {
    
    private final NoteDao noteDao;
    private final LiveData<List<Note>> allNotes;
    private final LiveData<List<Note>> archivedNotes;
    private final LiveData<List<Note>> highPriorityNotes;
    
    public NoteRepository(Application application) {
        NoteDatabase database = NoteDatabase.getInstance(application);
        noteDao = database.noteDao();
        allNotes = noteDao.getAllNotes();
        archivedNotes = noteDao.getArchivedNotes();
        highPriorityNotes = noteDao.getHighPriorityNotes();
    }
    
    // 数据操作方法
    
    public void insert(Note note) {
        new InsertNoteAsyncTask(noteDao).execute(note);
    }
    
    public void update(Note note) {
        new UpdateNoteAsyncTask(noteDao).execute(note);
    }
    
    public void delete(Note note) {
        new DeleteNoteAsyncTask(noteDao).execute(note);
    }
    
    public void deleteAllNotes() {
        new DeleteAllNotesAsyncTask(noteDao).execute();
    }
    
    // 获取数据的方法
    
    public LiveData<List<Note>> getAllNotes() {
        return allNotes;
    }
    
    public LiveData<List<Note>> getArchivedNotes() {
        return archivedNotes;
    }
    
    public LiveData<List<Note>> getHighPriorityNotes() {
        return highPriorityNotes;
    }
    
    public LiveData<List<Note>> getNotesByCategory(String category) {
        return noteDao.getNotesByCategory(category);
    }
    
    public LiveData<List<Note>> searchNotes(String query) {
        return noteDao.searchNotes(query);
    }
    
    // 异步任务类，用于在后台线程执行数据库操作
    
    private static class InsertNoteAsyncTask extends AsyncTask<Note, Void, Void> {
        private final NoteDao noteDao;
        
        private InsertNoteAsyncTask(NoteDao noteDao) {
            this.noteDao = noteDao;
        }
        
        @Override
        protected Void doInBackground(Note... notes) {
            noteDao.insert(notes[0]);
            return null;
        }
    }
    
    private static class UpdateNoteAsyncTask extends AsyncTask<Note, Void, Void> {
        private final NoteDao noteDao;
        
        private UpdateNoteAsyncTask(NoteDao noteDao) {
            this.noteDao = noteDao;
        }
        
        @Override
        protected Void doInBackground(Note... notes) {
            noteDao.update(notes[0]);
            return null;
        }
    }
    
    private static class DeleteNoteAsyncTask extends AsyncTask<Note, Void, Void> {
        private final NoteDao noteDao;
        
        private DeleteNoteAsyncTask(NoteDao noteDao) {
            this.noteDao = noteDao;
        }
        
        @Override
        protected Void doInBackground(Note... notes) {
            noteDao.delete(notes[0]);
            return null;
        }
    }
    
    private static class DeleteAllNotesAsyncTask extends AsyncTask<Void, Void, Void> {
        private final NoteDao noteDao;
        
        private DeleteAllNotesAsyncTask(NoteDao noteDao) {
            this.noteDao = noteDao;
        }
        
        @Override
        protected Void doInBackground(Void... voids) {
            noteDao.deleteAllNotes();
            return null;
        }
    }
} 